//
//  BaseTableViewCell.swift
//  IMFrame
//
//  Created by Author on 2023/9/20.
//

import UIKit
import SnapKit
let SCREENW =  UIScreen.main.bounds.size.width
let SCREENH = UIScreen.main.bounds.size.height
class BaseTableViewCell: UITableViewCell {
    /*
     event: ChatEvenType枚举类型的事件
     msgModel: 消息模型
     */
    var eventCallback:((_ event: ChatEvenType, _ msgModel: MessageModel)->Void)?
    var msg: MessageModel?{
        didSet {
            self.updateData()
        }
    }
    private(set) lazy var containerView :UIView = {
        let view = UIView()
        return view
    }()
    
    private(set) var wrapTop = 24
    
    var wrapLeading:ConstraintMakerEditable?
    
    private(set) lazy  var wrapView: UIView = {
        let view = UIView()
        return view
    }()

    private(set) lazy var sendBubbleImgView :UIImageView = {
        let imgView = UIImageView()
        var image = UIImage(named: "sendBubble")
        imgView.image = image?.stretchableImage(withLeftCapWidth: Int(((image?.size.width ?? 0) - 8)*0.5) , topCapHeight: Int((image?.size.height ?? 0)*0.5))
        imgView.isUserInteractionEnabled = true
        imgView.isHidden = true
        return imgView
    }()
    
    
    
    private(set) lazy var timeLabel :UILabel = {
        let label = UILabel()
        label.isHidden = true
        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: 12)
        label.textColor = UIColor.white.withAlphaComponent(30)
        return label
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.backgroundColor = .clear
        self.contentView.backgroundColor = .clear
        self.clipsToBounds = true
        self.layer.masksToBounds = true
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        contentView.addSubview(containerView)
        
        
        
        containerView.snp.makeConstraints { make in
            make.width.top.bottom.equalToSuperview()
            make.leading.equalTo(0)
        }
        contentView.addSubview(timeLabel)
        timeLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(24)
            make.height.equalTo(16)
        }
        containerView.addSubview(sendBubbleImgView)
        
        containerView.addSubview(wrapView)
        wrapView.snp.makeConstraints { make in
            make.top.equalTo(24)
            make.width.equalTo(0)
            make.bottom.equalToSuperview().offset(0)
            make.leading.equalTo(15)
            make.trailing.equalTo(-15)
        }
        
        sendBubbleImgView.snp.makeConstraints { make in
            make.trailing.equalTo(-15)
            make.bottom.equalTo(wrapView.snp.bottom)
            make.top.equalTo(wrapView.snp.top)
            make.leading.equalTo(wrapView.snp.leading).offset(-12)
            
        }
        
    }
    private func currentTime()->String{
        // 创建一个DateFormatter对象
        let dateFormatter = DateFormatter()
        
        // 设置时区为北京时区
        dateFormatter.timeZone = TimeZone(identifier: "Asia/Shanghai")
        
        // 设置日期格式为"yyyy-MM-dd'T'HH:mm:ssZ"
        dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        
        // 获取当前日期和时间
        let currentDate = Date()
        
        // 使用DateFormatter将日期和时间格式化为字符串
        let currentDateString = dateFormatter.string(from: currentDate)
        return currentDateString
        
    }
    
    
    func updateData(){
        guard let msg = msg else{return}
        wrapTop = 24
        //show time
        if msg.isShowTime{
            //设置时间
            self.timeLabel.text = currentTime()
            self.timeLabel.isHidden = false
            wrapTop = 64
        }else{
            self.timeLabel.isHidden = true
        }
        
        sendBubbleImgView.isHidden = false
        
        wrapView.snp.remakeConstraints { make in
            make.top.equalTo(wrapTop)
            if msg.messageType == .voice {
                make.width.equalTo(0)
            }
            make.bottom.equalToSuperview()
            make.trailing.equalTo(-35)
        }
        
        
        
        
        
        
    }
    
    
}


/*
 let longTap = UILongPressGestureRecognizer(target: self, action: #selector(longClick))
 self.addGestureRecognizer(longTap)
 }
 
 
 //        @objc func longClick() {
 //            self.eventCallback?(.CellLongTouch, self.msg)
 //        }
 //
 //        func hiddenBubble(){
 //            recivedBubbleImgView.isHidden = true
 //            sendBubbleImgView.isHidden = true
 //        }
 //
 */
